<?php
	require("libraries/student.php");
	class Rooms extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_room");
			$this->load->library("string");
			$this->load->helper(array("form","date"));
			$this->load->library("form_validation");	
		}
		public function index(){
			$config['base_url'] = base_url().'admin/rooms/index/';
			$config['total_rows'] = $this->model_room->count_all();
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$config['first_link'] = "First";
			$config['last_link'] = "Last";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['title']= "Manage rooms";
			$data['act'] = 3;
			$data['listship'] = $this->model_room->listship();
			$data['data']['info']= $this->model_room->list_all($config['per_page'],$start);
			$data['template'] = "rooms/list_room";
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['error'] = "";
			$data['data'] = "";
			$data['act'] = 3;
			$data['title'] = "Manage rooms";
			$data['template'] = "rooms/add_room";
			$data['listship'] = $this->model_room->listship();
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("room_title","Room title","min_length[5]|require");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('room_title'));
					  $url = str_replace(' ', '-',strtolower($this->string->replace($title)));
					  $room_startdate = strtotime($this->input->post("room_startdate"));
					  $room_enddate   = strtotime($this->input->post("room_enddate"));
					  if($room_startdate > $room_enddate){
						  die("Start date must be before return date");
					  }
					  $db = array(
							"room_title" => $this->input->post("room_title"),
							"room_rewrite" => $url,
							"room_promo_single1" => $this->input->post("room_promo_single1"),
							"room_promo_single2" => $this->input->post("room_promo_single2"),
							"room_promo_double1" => $this->input->post("room_promo_double1"),
							"room_promo_double2" => $this->input->post("room_promo_double2"),		
							"room_price_single" => $this->input->post("room_price_single"),
							"room_price_double" => $this->input->post("room_price_double"),
							"room_price_child" => $this->input->post("room_price_child"),
							"room_price_infant" => $this->input->post("room_price_infant"),
							"room_startdate"	=> $room_startdate,
							"room_enddate" 		=> $room_enddate,
							"room_number" => $this->input->post("room_number"),
							"sheep_id" => $this->input->post("sheep_id")
					  );
					if($_FILES['img']['name'] != NULL){
						$config['upload_path'] = './uploads/sheep/rooms';
						$config['allowed_types'] = 'gif|jpg|jpeg|png';
						$config['max_size']	= '2000';
						$config['max_width']  = '1400';
						$config['max_height']  = '1400';
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("img")){
							$data = array('error' => $this->upload->display_errors());
							$data['data'] = "";
							$data['act'] = 3;
							$data['title'] = "Manage rooms";
							$data['template'] = "rooms/add_room";
							$data['listship'] = $this->model_room->listship();
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['room_image'] = $data['file_name'];
							$this->createThumbnail("uploads/sheep/rooms",$db['room_image'],95,65);
						}
					 }
					 $this->model_room->add($db);
					 redirect(base_url()."admin/rooms/index");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function check_news($news){
			$id = $this->uri->segment(4);
			if($this->model_room->check_news($news,$id) == FALSE){
				$this->form_validation->set_message("check_news","News this already exist!");
				return FALSE;
			}else{
				return TRUE;
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['error'] = "";
			$data['data'] = "";
			$data['act'] = 3;
			$data['title'] = "Manage rooms";
			$data['template'] = "rooms/edit_room";
			$data['listship'] = $this->model_room->listship();
			$data['getdata'] = $this->model_room->getdata($id);
			$data['ship_id'] = $data['getdata']['sheep_id'];
			if($data['getdata'] == NULL){
				die("Error!");
			}
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("room_title","Room title","min_length[5]|require");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('room_title'));
					  $url = str_replace(' ', '-',strtolower($this->string->replace($title)));
					  $room_startdate = strtotime($this->input->post("room_startdate"));
					  $room_enddate   = strtotime($this->input->post("room_enddate"));
					  if($room_startdate > $room_enddate){
						  die("Start date must be before return date");
					  }
					  $db = array(
							"room_title" => $this->input->post("room_title"),
							"room_rewrite" => $url,
							"room_promo_single1" => $this->input->post("room_promo_single1"),
							"room_promo_single2" => $this->input->post("room_promo_single2"),
							"room_promo_double1" => $this->input->post("room_promo_double1"),
							"room_promo_double2" => $this->input->post("room_promo_double2"),	
							"room_price_single" => $this->input->post("room_price_single"),
							"room_price_double" => $this->input->post("room_price_double"),
							"room_price_child" => $this->input->post("room_price_child"),
							"room_price_infant" => $this->input->post("room_price_infant"),
							"room_startdate"	=> $room_startdate,
							"room_enddate" 		=> $room_enddate,
							"room_number" => $this->input->post("room_number"),
							"sheep_id" => $this->input->post("sheep_id")
					  );
					  if($_FILES['img']['name'] != NULL){
						$config['upload_path'] = './uploads/sheep/rooms';
						$config['allowed_types'] = 'gif|jpg|jpeg|png';
						$config['max_size']	= '2000';
						$config['max_width']  = '1400';
						$config['max_height']  = '1400';
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("img")){
							$data = array('error' => $this->upload->display_errors());
							$data['data'] = "";
							$data['act'] = 3;
							$data['title'] = "Manage rooms";
							$data['template'] = "rooms/edit_room";
							$data['listship'] = $this->model_room->listship();
							$data['getdata'] = $this->model_room->getdata($id);
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['room_image'] = $data['file_name'];
							$this->createThumbnail("uploads/sheep/rooms",$db['room_image'],95,65);
						}
					 }
					 $this->model_room->update($db,$id);
					 redirect(base_url()."admin/rooms/index");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function search(){
			$title = $this->input->post("val");
			if($title == NULL){
				echo "Bạn phải nhập tiêu đề tin!";
			}else{
				$data  = $this->model_room->search_ajax($title);
				if($data == NULL){
					echo "Không tìm thấy tin tức nào!";
				}else{
					echo '<table cellpadding="0" cellspacing="0" width="905">
									<tbody><tr>
									  <th width="23">Stt.</th>
									  <th width="300">Tiêu đề tin</th>
									  <th width="136">Tác giả</th>
									  <th width="132">Ngày đăng</th>
									  <th width="64">Actions</th>
									</tr>';
					$stt = 0;
					foreach($data as $items){
						$stt++;
						echo "<tr>";	
						echo "<td>$stt</td>";
						echo "<td>".$items['room_title']."</td>";
						echo "<td>".$items['room_author']."</td>";
						echo "<td>".$items['room_date']."</td>";
						echo "<td>";
							echo "<div class='actions_menu'><ul>";
								echo "<li><a href='".base_url()."admin/news/update/$items[room_id]/' class='edit'>Edit</a></li>";
								echo "<li><a href='".base_url()."admin/news/del/$items[room_id]/' class='delete' onclick='return check()'>Del</a></li>";
							echo "</ul></div>";
						echo "</td>";

						echo "</tr>";
					}
					echo "</tbody></table>";
				}
			}
		}
		public function select(){
			$val = $this->input->post("val");
			$data = $this->model_room->listroom($val);
			if($data != NULL){
				echo '<table cellpadding="0" cellspacing="0" width="905">
									<tbody><tr>
									  <th width="26">Stt.</th>
									  <th width="344">Room title</th>
									  <th width="121">Price single</th>
                                      <th width="121">Price double</th>
									  <th width="123">Total room</th>
                                      <th width="214">Ship name</th>
									  <th width="75">Actions</th>
									</tr>';
				$stt = 0;
				foreach($data as $items){
					$stt++;
					echo "<tr>";	
						echo "<td>$stt</td>";
							echo "<td>".$items['room_title']."</td>";
							echo "<td>".$items['room_price_single']."</td>";
							echo "<td>".$items['room_price_double']."</td>";
							echo "<td>".$items['room_number']."</td>";
							echo "<td>".$items['sheep_name']."</td>";
							echo "<td>";
								echo "<div class='actions_menu'><ul>";
									echo "<li><a href='".base_url()."admin/rooms/update/$items[room_id]/' class='edit'>Edit</a></li>";
									echo "<li><a href='".base_url()."admin/rooms/del/$items[room_id]/' class='delete' onclick='return check()'>Del</a></li>";
								echo "</ul></div>";
							echo "</td>";
					echo "</tr>";
				}
				echo "</table>";
			}else{
				echo "No result";
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_room->getdata($id);
			/*echo "<pre>";
			print_r($data);
			echo "</pre>";
			die();*/
			@unlink("uploads/sheep/rooms/".$data['room_image']);
			@unlink("uploads/sheep/rooms/thumb".$data['room_image']);
			$this->model_room->del($id);
			redirect(base_url()."admin/rooms");
		}
		public function createThumbnail($parth,$fileName,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = $parth.'/'.$fileName;
			$config['new_image'] = $parth.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}